package com.gxa.usermanagement.service.impl;

import com.gxa.common.R;
import com.gxa.common.entity.usermange.Distribution;
import com.gxa.common.entity.usermange.DistributionAuditing;
import com.gxa.usermanagement.mapper.DistributionMapper;
import com.gxa.usermanagement.service.DistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DistributionServiceImpl implements DistributionService {

    @Autowired
    private DistributionMapper distributionMapper;

    @Override
    public R addDistributionService(Distribution distribution) {
        R r = new R();
        distributionMapper.addDistributionMapper(distribution);
        r.setCode(200);
        r.setMessage("添加成功");

        return r;
    }

    @Override
    public R findDistributionByIdService(Integer id) {
        R r = new R();
        List<Distribution> list = distributionMapper.findDistributionByIdMapper(id);
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R findDistributionAllService(Integer currentPage, Integer pageSize) {
        R r = new R();
        Map map = new HashMap<>();
        Integer start = (currentPage-1)*pageSize;
        Integer off  = pageSize;
        List<Distribution> list = distributionMapper.findDistributionAllMapper(start,off);
        Integer count = distributionMapper.countDistribution();
        map.put("distribution",list);
        map.put("count",count);

        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(map);

        return r;
    }

    /**
     * 锁定 解锁配送站
     * @param distribution
     * @param distributionAuditing
     * @return
     */
    @Override
    public R updateDistributionService(Distribution distribution, DistributionAuditing distributionAuditing) {
        distributionMapper.updateDistributionMapper(distribution.getState(),distribution.getId());
        if(distributionAuditing!=null){
            distributionMapper.addAuditingMapper(distributionAuditing);
        }
        R r = new R();
        r.setMessage("操作成功");
        return r;
    }


}
